<?php

namespace Users\Controller;

use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\LoginForm;


class LoginController extends AbstractActionController
{   
	protected $form;
	protected $storage;
	protected  $authservice;
	
	public function indexAction()
	{
		$form = new LoginForm();
		$this->form = $form;
		$viewModel = new ViewModel(array('form' =>$form));
		//$viewModel->setCaptureTo('sidebar');
		return $viewModel ;
	}
	
	public function getAuthService()
	{
		if (! $this->authservice) {
			$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
			$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,'user','email','password', 'MD5(?)');
			$authService = new AuthenticationService();
			$authService->setAdapter($dbTableAuthAdapter);
			$this->authservice = $authService;
		}
		return $this->authservice;
	}
	
	public function processAction()
	{
	  $this->getAuthService()->getAdapter()->setIdentity($this->request->getPost('email'))->setCredential($this->request->getPost('password'));
	  $result = $this->getAuthService()->authenticate();
	   if ($result->isValid()) {
		$this->getAuthService()->getStorage()->write($this->request->getPost('email'));
		return $this->redirect()->toRoute(NULL , array(
				'controller' => 'login',
				'action' => 'confirm'
		));
	   }
	   else {
	   	$model = new ViewModel(array(
	   			'error' => true,
	   			'form' => $this->form,
	   	));
	   	//$this->form->
	   	$model->setTemplate('users/login/index');
	   	return $model;
	   }
    }
    
    public function confirmAction()
    {
    	$user_email = $this->getAuthService()->getStorage()->read();
    	$viewModel = new ViewModel(array(
    			'user_email' => $user_email
    	));
    	return $viewModel;
    }
    
    public function getSessionStorage()
    {
    	if (! $this->storage) {
    		$this->storage = $this->getServiceLocator()
    		->get('Users\Model\MyAuthStorage');
    	}
    	 
    	return $this->storage;
    }
    public function logoutAction() {
    	//$this->view->layout()->disableLayout();
    	$this->getSessionStorage()->forgetMe();
        $this->getAuthService()->clearIdentity();     
        $this->flashmessenger()->addMessage("You've been logged out");
        return $this->redirect()->toRoute('home');
    }
//*************************************************login in sidebar***********************************
    public function process2Action()
    {
    	$this->getAuthService()->getAdapter()->setIdentity($this->request->getPost('email'))->setCredential($this->request->getPost('password'));
    	$result = $this->getAuthService()->authenticate();
    	if ($result->isValid()) {
    		$this->getAuthService()->getStorage()->write($this->request->getPost('email'));
    		return $this->redirect()->toRoute(NULL , array(
    				'controller' => 'login',
    				'action' => 'confirm2'
    		));
    	}
    	else {
    		$model = new ViewModel(array(
    				'error' => true,
    				'form' => $this->form,
    		));
    		//$this->form->
    		$model->setTemplate('users/login/index');
    		return $model;
    	}
    }
    
    public function confirm2Action()
    {
    	$user_email = $this->getAuthService()->getStorage()->read();
    	$viewModel = new ViewModel(array(
    			'user_email' => $user_email
    	));
    	$viewModel->setCaptureTo('sidebar');
    	return $viewModel;
    }
}
?>